<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'edit';
$merchid=$this->checklogin();
$return['steps']=array(
	array('title'=>'选择时间','en_title'=>'basic info'),
	array('title'=>'结算消息','en_title'=>'Shareholders'),
	array('title'=>'创建结算单','en_title'=>'Team info'),
);
if($_RQ['op']=='edit'){
	$return['step']=0;
	$postdata=get_postdata();
	if($postdata){
		if(!$postdata['year']){
			exi('请选择年份','error');
		}
		if(!$postdata['month']){
			exi('请选择月份','error');
		} 
		if($postdata['month']<10){
			$postdata['month']='0'.$postdata['month'];
		}
		if(!$postdata['week']){
			$firstday = strtotime($postdata['year'] .$postdata['month'] . "01000000");
			$firstdaystr = date("Y-m-01", $firstday);
			$lastday = strtotime(date('Y-m-d 23:59:59', strtotime("$firstdaystr +1 month -1 day")));
		}else{
			if($postdata['week']<4){
				$day=($postdata['week']-1)*7+1;		
				if($day<10){
					$day='0'.$day;
				}
				$firstday = strtotime($postdata['year'] .$postdata['month'] .$day. "000000");
				$firstdaystr = date("Y-m-d", $firstday);
				$lastday = strtotime(date('Y-m-d 23:59:59', strtotime("$firstdaystr  +6 day")));
			}else{
				$firstday = strtotime($postdata['year'] .$postdata['month'] . "22000000");
				$firstdaystr = date("Y-m-01", $firstday);
				$lastday = strtotime(date('Y-m-d 23:59:59', strtotime("$firstdaystr +1 month -1 day")));
			}
		}
		$ordergoods=pdo_getall('jy_weishop_order_goods',array('status'=>3,'createtime between'=>array($firstday,$lastday),'uniacid'=>$_SESSION['uniacid'],'api_id'=>$merchid,'api_pluginname'=>'jy_supplier','clearstatus'=>0),array('id','orderid','goodsid','price','total','total_price','fee_desc'));
		if($ordergoods){
			$user=array();
			$salsecount=0;
			$item['ordermoney']=0;
			$item['commission_money']=0;
			$item['sys_choufee']=0;
			foreach($ordergoods as $k=>$v){
				$orderids[]=$v['orderid'];
				if($this->plugin_exists('jy_commission')){
					$commission_money=model('jy_commission/commission')->get_goodsfee($v['goodsid'],$v['orderid'],$v['id']);
				}
				$salsecount +=$v['total'];
				if(!$v['fee_desc']){
					model('jy_weishop/order')->setfee_desc($v['orderid'],true);
					$v['total_price']=pdo_getcolumn('jy_weishop_order_goods',array('id'=>$v['id']),'total_price');
				}
				$item['ordermoney'] +=$v['total_price'];
				$item['commission_money'] +=$commission_money;
			}
			foreach($orderids as $orderid){
				$merchorder=$this->model('commission')->get_choufee($orderid,true,$merchid,1);
				$item['sys_choufee'] +=$merchorder['commission_money'];
			}
		}	
		if(!$salsecount){
			exi('没有可结算订单','error');
		}
		$item['starttime']=$firstday;
		$item['endtime']=$lastday;
		//$item['deductprice']=;
		//$item['discountprice']=;
		$item['salsecount']=$salsecount;
		$item['realprice']=$item['ordermoney']?$item['ordermoney']:0;
		$item['price']=$item['realprice'];
		$setting=$this->plugin_setting('setting');
		$item['rate']=intval($setting['rate']);
		$item['ratefee']=$item['price']*$item['rate']/100;
		$item['realpricerate']=$item['price']-$item['ratefee'];
		if($setting['educt_commission']==1){
			$item['realpricerate']=$item['realpricerate']-$item['commission_money'];
			$item['educt_commission']=1;
		}
		$item['finalprice']=$item['realpricerate'];		
		$item['finalprice']=$item['finalprice']-$item['sys_choufee'];
		$this->session('item',$item);
		exi('','','step1');
	}
	$year=intval(date('Y',time()));
	$return['year']=array('0'=>array('id'=>$year-9,'title'=>$year-9),'1'=>array('id'=>$year-8,'title'=>$year-8),'2'=>array('id'=>$year-7,'title'=>$year-7),'3'=>array('id'=>$year-6,'title'=>$year-6),'4'=>array('id'=>$year-5,'title'=>$year-5),'5'=>array('id'=>$year-4,'title'=>$year-4),'6'=>array('id'=>$year-3,'title'=>$year-3),'7'=>array('id'=>$year-2,'title'=>$year-2),'8'=>array('id'=>$year-1,'title'=>$year-1),'9'=>array('id'=>$year,'title'=>$year));
	$return['month']=array('0'=>array('id'=>1,'title'=>1),'1'=>array('id'=>2,'title'=>2),'2'=>array('id'=>3,'title'=>3),'3'=>array('id'=>4,'title'=>4),'4'=>array('id'=>5,'title'=>5),'5'=>array('id'=>6,'title'=>6),'6'=>array('id'=>7,'title'=>7),'7'=>array('id'=>8,'title'=>8),'8'=>array('id'=>9,'title'=>9),'9'=>array('id'=>10,'title'=>10),'10'=>array('id'=>11,'title'=>11),'11'=>array('id'=>12,'title'=>12));
	$return['week']=array('0'=>array('id'=>1,'title'=>'第一周'),'2'=>array('id'=>2,'title'=>'第二周'),'2'=>array('id'=>3,'title'=>'第三周'),'3'=>array('id'=>4,'title'=>'第四周'));
	$htmls=array(
			'type'=>'edit',
			'edit'=>array(
				array('type'=>'tip','title'=>'<red>只有完成的订单才会被结算！</red>'),
				array('name'=>'steps','type'=>'steps'),
				array('name'=>'year','title'=>'选择年份','type'=>'select','options'=>'year'),
				array('name'=>'month','title'=>'选择月份','type'=>'select','options'=>'month'),
			)
	);
	if($_SESSION['os']=='mobile'){
		$return['htmls']=$this->template_url('manage/apply_add');
	}else{
		$return['htmls']=$htmls;
	}
	exi($return);
}
if($_RQ['op']=='step1'){
	$return['step']=1;
	$return['pay_types']=$this->pay_types;
	$item=$this->session('item');
	$item['ratefee']=price($item['ratefee']);
	$item['commission_money']=price($item['commission_money']);
	$item['sys_choufee']=price($item['sys_choufee']);
	$item['finalprice']=price($item['finalprice']);
	$return['item']=$item;
	$htmls=array(
				'type'=>'edit',
				'post'=>'step2',
				'edit'=>array(
					array('name'=>'pay_type','title'=>'打款方式','type'=>'radio','options'=>'pay_types'),
					array('name'=>'salsecount','title'=>'商品销量','type'=>'unitstring','unit'=>'个','read'=>true),
					array('name'=>'ordermoney','title'=>'订单金额','type'=>'unitstring','unit'=>'元','read'=>true),
					array('name'=>'price','title'=>'提现金额','type'=>'unitstring','unit'=>'元','read'=>true),
					array('name'=>'rate','title'=>'提现费率','type'=>'unitstring','read'=>true,'unit'=>'点','help'=>'手续费百分比'),
					array('name'=>'ratefee','title'=>'提现手续费','type'=>'unitstring','unit'=>'元','read'=>true),
					array('name'=>'commission_money','title'=>'扣除佣金','type'=>'unitstring','unit'=>'元','read'=>true,'show'=>array('educt_commission'=>1),'help'=>'设置里面开启了扣除佣金，这次才会显示！【注意】：<red>只有存在上级或开启分销内购的订单</red>，并产生佣金时，才有效！'),
					array('name'=>'sys_choufee','title'=>'平台抽成','type'=>'unitstring','unit'=>'元','read'=>true,'help'=>'不同类型商户抽成比例不一样！'),
					array('name'=>'finalprice','title'=>'到账金额','type'=>'unitstring','unit'=>'元','read'=>true),
				)
	);
	if($_SESSION['os']=='mobile'){
		$return['htmls']=$this->template_url('manage/apply_add');
	}else{
		$return['htmls']=$htmls;
	}
	exi($return);
	
}
if($_RQ['op']=='step2'){
	$return['step']=2;
	$postdata=get_postdata();
	if($postdata['pay_type']=='alipay'){
			if(!$postdata['realname']){
				exi('请先填写真实姓名！','error');
			}
			if(!$postdata['alipay']){
				exi('请先填写支付宝帐号！','error');
			}
			if(!$postdata['alipay1']){
				exi('请确认帐号','error');
			}
			if($postdata['alipay1']!=$postdata['alipay']){
				exi('确认帐号与支付宝帐号不匹配！','error');
			}
			$typetitle='支付宝';
	}
	if($postdata['pay_type']=='union'){
			if(!$postdata['realname']){
				exi('请输入姓名','error');
			}
			if(!$postdata['bankname']){
				exi('请输入所属银行','error');
			}
			if(!$postdata['bankcard']){
				exi('请银行卡号','error');
			}
			if(!$postdata['bankcard1']){
				exi('请确认卡号','error');
			}
			if($postdata['bankcard']!=$postdata['bankcard1']){
				exi('确认卡号与银行卡号不匹配！','error');
			}
			$typetitle='银行卡';
	}
	$data=array(
		'clearno'=>get_ordersn('jy_supplier_clearing','clearno'),
		'starttime'=>$postdata['starttime'],
		'endtime'=>$postdata['endtime'],
		'realprice'=>$postdata['realprice'],
		'price'=>$postdata['price'],
		'realpricerate'=>$postdata['realpricerate'],
		'finalprice'=>$postdata['finalprice'],
		'pay_type'=>$postdata['pay_type'],
		'pay_trade_no'=>$postdata['pay_trade_no'],
		'realname'=>$postdata['realname'],
		'alipay'=>$postdata['alipay'],
		'bankname'=>$postdata['bankname'],
		'bankcard'=>$postdata['bankcard'],
		'status'=>0,
	);
	$data['uniacid']=$_SESSION['uniacid'];
	$data['merchid']=$merchid;
	$data['createtime']=time();
	pdo_insert('jy_supplier_clearing',$data);
	pdo_update('jy_weishop_order_goods',array('clearstatus'=>1),array('createtime between'=>array($postdata['starttime'],$postdata['endtime']),'uniacid'=>$_SESSION['uniacid'],'api_id'=>$merchid,'api_pluginname'=>'jy_supplier','clearstatus'=>0));
	pdo_update('jy_supplier_order',array('status'=>2),array('createtime between'=>array($postdata['starttime'],$postdata['endtime']),'uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'status'=>1));
	exi('创建结算单成功','','/manage.apply_list/status1/');
}